import {defineStore} from 'pinia';
import workApi from "@/http/workApi";

export const useTeacherStore = defineStore('teacher', {
    actions: {
        updateClass() {
            workApi.get('teacher/class').then((res) => {
                this.class = res.data.map((s) => {
                    s.label = s.class_name
                    s.value = s.id
                    return s
                })
            })
        },
        updateTrain(fun=() => {}) {
            workApi.get('teacher/training').then((res) => {
                this.train = res.data
            }).then(fun())
        },
        updateTask(fun=() => {}) {
            workApi.get('teacher/task').then((res) => {
                // 给返回值添加label 跟 value
                this.task = res.data.enable.map((s) => {
                    s.label = s.name
                    s.value = s.id + '-' + s.name
                    s.children.map((v) => {
                        v.label = v.name
                        v.value = v.task_id
                        return v
                    })
                    return s
                })
                fun()
            })
        },
        setAllGrade(value) {
            this.allGrade = value
        },
        updateClassGrade(training_id, class_id) {
            workApi.get("teacher/class_score", {
                params: {
                    training_id: training_id,
                    class_id: class_id
                }
            }).then(r => {
                this.classGrade = r.data
            })
        },
    },


    state: () => ({
        class: [],
        train: {
            enable: [],
            disable: []
        },
        task: [],
        allGrade: [],
        classGrade:[],
    }),

    getters: {
        getExtractionTask() {
            // 将任务从项目中提取出来
            let result = [];
            for (let i = 0; i < this.task.length; i++) {
                result = [...result, ...this.task[i].children]
            }
            return result

        }
    },
})
